﻿// <copyright file="BillsCosponsoredByMember.cs" company="Microsoft Corporation">
// Copyright (c) 2009 Microsoft Corporation All Rights Reserved
// </copyright>
// <author>Michael S. Scherotter</author>
// <email>mischero@microsoft.com</email>
// <date>2009-09-29</date>
// <summary>BillsCosponsoredByMembers class definition</summary>

namespace NYTimes
{
    using System;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Globalization;
    using System.Linq;
    using System.Xml.Linq;
    using NYTimes.Core;
    using NYTimes.Data;

    /// <summary>
    /// New York Times Congressional Bills Cosposnored by member API
    /// </summary>
    public class BillsCosponsoredByMember : CongressApi
    {
        #region Fields
        /// <summary>
        /// the Member Id for the Senator or Representative
        /// </summary>
        private string memberId;
        #endregion

        #region Constructors
        /// <summary>
        /// Initializes a new instance of the BillsCosponsoredByMember class
        /// </summary>
        public BillsCosponsoredByMember()
        {
            this.Bills = new ObservableCollection<Bill>();

            if (DesignerProperties.IsInDesignTool)
            {
                var xml = Core.SampleData.GetSampleData("CosponsorsByCongressMember.xml");

                if (!string.IsNullOrEmpty(xml))
                {
                    this.ParseXml(xml);
                }
            }
        }
        #endregion

        #region Properties
        /// <summary>
        /// Gets or sets the Member Id for the Senator or Representative
        /// </summary>
        public string MemberId 
        { 
            get
            {
                return this.memberId;
            }

            set
            {
                if (this.memberId != value)
                {
                    this.memberId = value;

                    this.TriggerPropertyChanged("MemberId");

                    this.FetchResults();
                }
            }
        }

        /// <summary>
        /// Gets the Bills collection
        /// </summary>
        public ObservableCollection<Bill> Bills { get; private set; }

        #endregion

        #region Methods
        /// <summary>
        /// Clear the Bills list
        /// </summary>
        public override void ClearResults()
        {
            this.Bills.Clear();
        }

        /// <summary>
        /// returns true if the MemberId is valid
        /// </summary>
        /// <returns>true if the MemberId is valid</returns>
        public override bool ShouldFetchResults()
        {
            return !string.IsNullOrEmpty(this.MemberId);
        }

        /// <summary>
        /// Generate the service URL
        /// </summary>
        /// <param name="baseUri">the base URI</param>
        /// <returns>the full service URL</returns>
        public override Uri GenerateUrl(string baseUri)
        {
            var uriString = string.Format(
                CultureInfo.InvariantCulture,
                "{0}/members/{1}/bills/cosponsored.xml?api-key={2}",
                baseUri, 
                this.MemberId, 
                this.ApiKey);

            return new Uri(uriString, UriKind.Absolute);
        }

        /// <summary>
        /// Parse the results
        /// </summary>
        /// <param name="results">the results element</param>
        public override void OnParseResults(XElement results)
        {
            if (results == null)
            {
                throw new ArgumentNullException("results");
            }

            var bills = from bill in results.Element("bills").Elements("bill")
                        select new Bill(bill, 0);

            foreach (var bill in bills)
            {
                this.Bills.Add(bill);
            }
        }
        #endregion
    }
}
